package com.seven.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author seven
 * @Date 2020/5/11 15:54
 **/
@Service
public class SendEMailService {
    @Autowired
    JavaMailSenderImpl javaMailSender;
    public void send(String sendTo,String name){
        //复制的邮件发送，发送图片啥的
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = null;
            try {
                helper = new MimeMessageHelper(mimeMessage,true);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            //邮件设置
            helper.setSubject("欢迎激活");
            helper.setText("<h1>能兼容html标签</h1><p>你好 springBoot mail!</p>\n" +
                    "<hr>\n" +
                    "<a href='http://192.168.4.6:8080/active?name='"+name+">点击激活</a>" +
                    "<img src=\"https://spring.io/images/spring-logo-9146a4d3298760c2e7e49595184e1975.svg\">",true);
            //上传文件
            helper.addAttachment("my.png",new File("D:\\图片\\截图\\自动保存\\Snipaste_2020-04-22_14-03-09.png"));
            //发送目标
            helper.setTo(sendTo);
            helper.setFrom("1399435579@qq.com");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
